﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace CommonLib.SystemInfo
{
    public class IpInfo
    {
        /// <summary>
        /// 获取ip v4地址
        /// </summary>
        /// <returns></returns>
        public string GetIpAddress4()
        {
            try
            {
                string hostName = Dns.GetHostName();
                IPHostEntry iPHostEntry = Dns.GetHostEntry(hostName);
                var addressV = iPHostEntry.AddressList.FirstOrDefault(q => q.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);//ip4地址
                if (addressV != null)
                    return addressV.ToString();
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public string GetIpAddress6()
        {
            try
            {
                string hostName = Dns.GetHostName();
                IPHostEntry iPHostEntry = Dns.GetHostEntry(hostName);
                var addressV = iPHostEntry.AddressList.FirstOrDefault(q => q.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);//ip6地址
                if (addressV != null)
                    return addressV.ToString();
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}